<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\OperationLogs;
use App\Model\Version;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class VersionController extends BaseController
{
    public function __construct(Request $request, Version $version)
    {
        parent::__construct($request);
        $this->model = $version;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return $this->toJson(self::SUCCESS, $this->model->getList($request));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
           'version_id' => 'required|string',
            'describe' => 'required',
            'platform_id' => 'required',Rule::in(1,2),
            'download_url' => 'required|url',
            'channel_id' => 'required|string',
            'upgrade_type' => 'required',Rule::in(0,1,2),
            'audit_switch' => 'required', Rule::in(0,1)
        ]);
        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }
        $data = array_merge($request->except('_url','token'), ['create_time' => date('Y-m-d H:i:s')]);
        $res = $this->model->createData($data);
        if($res['code'] !== 0){
            return $this->toJson(self::ERROR, $res);
        }
        $data['id'] = $res['data']['id'];
        $data['platform_id'] = intval($data['platform_id']);
        $data['upgrade_type'] = intval($data['upgrade_type']);
        OperationLogs::createData($request,OperationLogs::VERSION, '新增');
        return $this->toJson(self::SUCCESS,$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'version_id' => 'required|string',
            'describe' => 'required',
            'platform_id' => 'required',Rule::in(1,2),
            'download_url' => 'required|url',
            'channel_id' => 'required',
            'upgrade_type' => 'required',Rule::in(0,1,2),
            'audit_switch' => 'required', Rule::in(0,1)
        ]);
        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }
        $res = $this->model->updateData($request->only('id','version_id','describe','platform_id','download_url','channel_id','upgrade_type','audit_switch'));
        if($res['code'] !== 0){
            return $this->toJson(self::ERROR, $res);
        }
        OperationLogs::createData($request,OperationLogs::VERSION, '编辑', $id);
        return $this->toJson(self::SUCCESS, $res);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id)
    {
        $res = $this->model->removeData(['id'=>$id]);
        if($res['code'] !== 0){
            return $this->toJson(self::ERROR, $res);
        }
        OperationLogs::createData($request,OperationLogs::VERSION, '删除', $id);
        return $this->toJson(self::SUCCESS, $this->model->where(['id' => $id])->delete());
    }
}
